#include <iostream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <queue>
using namespace std;
void solve();
string S;
unordered_map<string, int> map;
int pos;
int main() {
    map.insert({"0 1 2 3 4 5 6 7", 0});
    ios::sync_with_stdio(false);
    while (getline(cin, S)) {
        solve();   
    }
}

struct State {
    string s;
    int pace;
    int index;
};

int dx[] = {
    2, -2 , 8, -8
};

bool valid(int x) {
    return x < S.length() && x >= 0;
}

void solve() {
    if (map.count(S) > 0) {
        cout << map[S] << endl;
        return;
    }
    unordered_set<string> set;
    queue<State> q;
    q.push({"0 1 2 3 4 5 6 7", 0, 0});
    while (!q.empty()) {
        State t = q.front(); q.pop();
        if (t.s == S) {
            cout << t.pace << endl;
            return;
        }

        for (int i = 0; i < 4; i++) {
            if (t.index == 6 && dx[i] == 2) continue;
            if (t.index == 8 && dx[i] == -2) continue;
            int ni = t.index + dx[i];
            if (valid(ni)) {
                string ss(t.s);
                swap(ss[t.index], ss[ni]);
                if (map.count(ss) == 0) map.insert({ss, t.pace + 1});
                if (set.count(ss) == 0) {
                    q.push({ss, t.pace + 1, ni});
                    set.insert(ss);
                }
            }
        }

    }
    cout << -1 << endl;
}